package cn.wolfcode.shiro;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component()
public class CrmRealm extends AuthorizingRealm {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }


    //跟认证有关的
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
       //通过token 获取用户名(用户登录的时候传的)
        Object username = token.getPrincipal();
        Employee employee = employeeMapper.selectByName((String) username);

        //判断是否存在数据库
        if (employee != null){
            //这里提供的是正确的用户名和密码
          return new SimpleAuthenticationInfo(employee,employee.getPassword(),
                    ByteSource.Util.bytes(username),"CrmRealm");
            //return new SimpleAuthenticationInfo(employee,employee.getPassword(), "CrmRealm");
        }
        return null;
    }
    //授权相关的
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获取当前登录的用户
        Subject subject = SecurityUtils.getSubject();
        //获取当前登录用户的信息,也就是对象
        Employee employee = (Employee) subject.getPrincipal();
      //判断当前用户是否为管理员
      if (employee.getAdmin()){
          info.addRole("ADMIN");
          info.addStringPermission("*:*");
      }else{
                //根据用户的id查询该用户拥有的角色
                List<Role> roles = roleMapper.selectByEmpId(employee.getId());
                ArrayList<String> roleSnList = new ArrayList<>();
                for (Role role: roles) {
                    roleSnList.add(role.getSn());
                }
                info.addRoles(roleSnList);
                //根据用户的id查询用户拥有的权限
                List<String> permissions = permissionMapper.selectByEmpId(employee.getId());
                info.addStringPermissions(permissions);
      }



        return info;
        //当前用户所拥有的角色
        //info.addRole("HR_MGR");
        //info.addStringPermissions(Arrays.asList( "employee:update","employee:delete"));

    }
}
